package net.alegen.tracker;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

public class CameraActivity
	extends Activity
	implements PictureCallback {    
	
    private Preview preview;
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);    
        this.preview = new Preview(this);
        this.setContentView(this.preview);
        
        this.preview.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CameraActivity.this.preview.getCamera().takePicture(null, null, CameraActivity.this);
			}
		});
    }

	public void onPictureTaken(byte[] data, Camera camera) {
		File imagesDirectory;
		File image;
		FileOutputStream outputStream;
		try {
			imagesDirectory = new File(new java.net.URI("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Tracker"));
			if(!imagesDirectory.exists())
				imagesDirectory.mkdir();
			
			image = new File(imagesDirectory.getAbsolutePath(), "TrackerImage " + System.currentTimeMillis());
			image.createNewFile();
			
			outputStream = new FileOutputStream(image);
			outputStream.write(data);
			outputStream.flush();
			outputStream.close();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}
}

class Preview
	extends SurfaceView
	implements SurfaceHolder.Callback {
    
	private SurfaceHolder surfaceHolder;
	private Camera camera;
    
    Preview(Context context) {
        super(context);

        this.surfaceHolder = this.getHolder();
        this.surfaceHolder.addCallback(this);
        //this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
    	this.camera = Camera.open();
        try {
        	this.camera.setPreviewDisplay(holder);
        } catch (IOException exception) {
        	this.camera.release();
        	this.camera = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    	this.camera.stopPreview();
    	this.camera.release();
    	this.camera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = this.camera.getParameters();
        parameters.setPreviewSize(w, h);
        this.camera.setParameters(parameters);
        this.camera.startPreview();
    }
    
    public Camera getCamera() {
    	return this.camera;
    }
}